﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using EdlinSoftware.RegexTester.Code;
using EdlinSoftware.RegexTester.Properties;

namespace EdlinSoftware.RegexTester
{
    public partial class SettingsForm : Form
    {
        #region Fields

        private bool _clearSavedRegularExpressions = false;

        #endregion

        #region Properties
        
        [Browsable(false)]
        public string RegexStorageFileName { get; set; }

        #endregion

        #region Constructors

        public SettingsForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods
        private void btnAddFileShortcut_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string encoding;
                switch (openFileDialog.FilterIndex)
                {
                    case 2:
                        {
                            encoding = "UTF8 encoding";
                            break;
                        }
                    case 3:
                        {
                            encoding = "Unicode encoding";
                            break;
                        }
                    default:
                        {
                            encoding = "Default encoding";
                            break;
                        }
                }

                new FileShortcutsProvider().AddFileShortcut(string.Format("{0} : {1}", encoding, openFileDialog.FileName));
                ShowAllFileShortcuts();
            }
        }

        private void btnRemoveFileShortcuts_Click(object sender, EventArgs e)
        {
            new FileShortcutsProvider().RemoveFileShortcuts(lbFileShortcuts.SelectedItems.OfType<string>());

            string[] shortcutsToDelete = lbFileShortcuts.SelectedItems.OfType<string>().ToArray();

            foreach (var shortcut in shortcutsToDelete)
            {
                lbFileShortcuts.Items.Remove(shortcut);
            }
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            ShowAllFileShortcuts();

            nudSizeOfRegexStore.Value = Settings.Default.SizeOfRegexStorage;
        }

        private void ShowAllFileShortcuts()
        {
            lbFileShortcuts.Items.Clear();
            lbFileShortcuts.Items.AddRange(new FileShortcutsProvider().GetAllFileShortcuts());
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            new FileShortcutsProvider().LoadShortcuts();
            Settings.Default.Reload();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            new FileShortcutsProvider().SaveShortcuts();
            Settings.Default.SizeOfRegexStorage = Convert.ToInt32(nudSizeOfRegexStore.Value);
            if (Settings.Default.SizeOfRegexStorage < 1)
            { Settings.Default.SizeOfRegexStorage = 1; }
            if (_clearSavedRegularExpressions)
            {
                new TextLinesStorage().SaveLinesToFile(RegexStorageFileName, new string[0]);
            }
            Settings.Default.Save();
        }

        private void btnClearRegex_Click(object sender, EventArgs e)
        {
            _clearSavedRegularExpressions = true;
        } 
        #endregion
    }
}
